const axios = require('axios');
const tunnel = require('tunnel')

const tunnelProxy = tunnel.httpsOverHttp({
  proxy: {
    host: '123.57.30.29',
    port: '16818',
    proxyAuth: 'yetao1109:bupipp9h'
  },
});

// create an axios instance
const service = axios.create({
  // baseURL: 'http://opendota.sanye666.top/api',
  baseURL:'https://api.opendota.com/api',
  // proxy: false,
  // httpsAgent: tunnelProxy,
  timeout: 60000
})

// request interceptor
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log(error) // for debug
    return Promise.reject('爷现在不想查（此月调用次数已上限）')
  }
)

// create an axios instance
const dota2service = axios.create({
  baseURL: 'http://localhost:3100/',
  timeout: 60000
})

// request interceptor
dota2service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
dota2service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// create an axios instance
const robotService = axios.create({
  baseURL: 'https://api.tianapi.com/txapi',
  timeout: 60000
})

// request interceptor
robotService.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
robotService.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

const stratzService = axios.create({
  baseURL: 'https://api.stratz.com/api/v1/',
  timeout: 60000
})

// request interceptor
stratzService.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
stratzService.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// create an axios instance
const newsService = axios.create({
  baseURL: 'https://api.tianapi.com',
  timeout: 60000
})

// request interceptor
newsService.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
newsService.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// create an axios instance
const opendotaService = axios.create({
  baseURL: 'http://opendota.sanye666.top/',
  timeout: 60000
})

// request interceptor
opendotaService.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
opendotaService.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

module.exports = {
  service,
  dota2service,
  robotService,
  stratzService,
  newsService,
  opendotaService
}
